﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PalcoCentral
{
    public partial class pcAdicionarCurso : Form
    {
        public pcAdicionarCurso()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
        }

       

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void tipoDiscBox_SelectedIndexChanged(object sender, EventArgs e) {
            if (tipoDiscBox.SelectedIndex == 0)
            {
                instTB.Enabled = true;
            }
            else {
                instTB.Enabled = false;
            }
        }

        private void salvarDiscBut_Click(object sender, EventArgs e)
        {
            Database.course course = new Database.course();
            try
            {
                course.courseType = Library.CourseType.getValue(this.tipoDiscBox.SelectedItem.ToString()).toStringDB();
            }
            catch
            {
                Library.Utils.errorFields("Tipo de curso inválido.", "Erro a inserir disciplina");
                return;
            } 
            if (!string.IsNullOrEmpty(this.nameAddP.Text))
            {
                course.courseName = this.nameAddP.Text;
            }
            else
            {
               Library.Utils.errorFields("Nome de disciplina inválido.","Erro a inserir disciplina");
                return;
            }
            int y = 0;
            if (int.TryParse(this.anoTB.Text, out y))
            {
                course.year = y;
            }
            else
            {
                Library.Utils.errorFields("Ano inválido.", "Erro a inserir disciplina");
                return;
            }
            course.courseDescription = this.descTextBox.Text;
            course.instrument = this.instTB.Text;
            course.observationsCourse = this.observationsAddP.Text;

            Library.OperationStatus os = Database.CourseDBAdapter.getInstance().addCourse(course);
            if (os.sucess)
            {
                Library.Utils.sucessFields("A disciplina foi inserida com sucesso.", "Inserir disciplina");
                this.Close();
                return;
            }
            else
            {
                Library.Utils.errorFields(os.errorMessage, "Erro a inserir disciplina");
            }

        }

        public void addSucessfull(string msg)
        {
            MessageBox.Show(string.Format("{0}", msg), "Operação bem sucedida",
                       MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }
        public void errorFields(string msg)
        {
            MessageBox.Show(string.Format("{0}", msg), "Erro nos dados introduzidos",
                       MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }


    }
}
